package com.dingding.common.core.push.factory.request.impl;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.daox.IBaseDaoX;
import com.dingding.common.core.beans.UserTaxiOrdert;
import com.dingding.common.core.beans.Userdetail;
import com.dingding.common.core.beans.special.Specialuserlogin;
import com.dingding.common.core.beans.special.UserspecialorderT;
import com.dingding.common.core.model.dto.PushModelBean;
import com.dingding.common.core.push.base.ClientAppType;
import com.dingding.common.core.push.base.ClientPushType;
import com.dingding.common.core.push.base.PushRequest;
import com.dingding.common.core.push.enums.RoleType;
import com.dingding.common.core.push.enums.push.SpecialDriverPushEnum;
import com.dingding.common.core.push.factory.PushBeanFactory;
import com.dingding.common.core.push.factory.PushDataFactory;
import com.dingding.common.core.push.factory.dtos.OrderDurationDTO;
import com.dingding.common.core.push.factory.request.IPushRequestFactory;
import com.dingding.common.core.push.pushbean.specialdriver.AppointSpecialOrderPushBean;
import com.dingding.common.core.push.pushbean.specialdriver.AppointTaxiOrderPushBean;
import com.dingding.common.core.push.pushbean.specialdriver.CustomerCancelSpecialOrderPush;
import com.dingding.common.core.push.pushmsg.TcpPushPack;
import com.dingding.common.core.util.JSONUtil;

/**
 * @Description:专车司机端的推送bean的生产者
 * @author fengshuonan
 * @date 2016年8月29日 下午2:43:14
 * @version 叮叮搭车
 * @since 2.4
 */
@Component("specialDriverPushRequestFactory")
public class SpecialDriverPushRequestFactory implements IPushRequestFactory {

	private Logger log = Logger.getLogger(this.getClass());

	@Resource(name = "baseDaoX")
	private IBaseDaoX baseDaoX;

	@Override
	public String createPushRequestString(String modular, String userId, Object... objects) {
		String result = null;

		if (SpecialDriverPushEnum.AppointSpecialOrderPush.toString().equals(modular)) {
			result = appointSpecialOrder(userId, (Userdetail) objects[0], (UserspecialorderT) objects[1],
					(OrderDurationDTO) objects[2]);
		} else if (SpecialDriverPushEnum.driverCancelSpecialOrderPush.toString().equals(modular)) {
			result = cancelSpecialOrder(userId, (CustomerCancelSpecialOrderPush) objects[0]);
		} else if (SpecialDriverPushEnum.AppointTaxiOrderPush.toString().equals(modular)) {
			result = appointTaxiOrder(userId, (Userdetail) objects[0], (UserTaxiOrdert) objects[1],
					(OrderDurationDTO) objects[2]);
		} else if (SpecialDriverPushEnum.TcpAppointSpecialOrderPush.toString().equals(modular)) {
			result = tcpAppointSpecial(userId, (Userdetail) objects[0], (UserspecialorderT) objects[1],
					(OrderDurationDTO) objects[2]);
		} else if (SpecialDriverPushEnum.TcpAppointTaxiOrderPush.toString().equals(modular)) {
			result = tcpAppointTaxi(userId, (Userdetail) objects[0], (UserTaxiOrdert) objects[1],
					(OrderDurationDTO) objects[2]);
		}
		return result;
	}
	
	/**
	 * @Description tcp方式推送给出租车车主订单
	 * @author fengshuonan
	 */
	public String tcpAppointTaxi(String userId, Userdetail userdetail, UserTaxiOrdert taxiOrdert,
			OrderDurationDTO orderDurationDTO){
		TcpPushPack tcpPushPack = new TcpPushPack();
		tcpPushPack.setTargetUserId(userId);
		tcpPushPack.setBizType((byte) 2);//出租车
		tcpPushPack.setModular((short) 0);//推送模块
		tcpPushPack.setCmd((short) 1);//指派订单
		AppointTaxiOrderPushBean pushBean = PushBeanFactory.appointTaxiOrderPushBean(userdetail, taxiOrdert,
				orderDurationDTO);
		tcpPushPack.setContent(JSONUtil.toJSONString(pushBean));
		return JSONUtil.toJSONString(tcpPushPack);
	}

	/**
	 * @Description tcp方式推送给专车车主订单
	 * @author fengshuonan
	 */
	public String tcpAppointSpecial(String userId, Userdetail userdetail, UserspecialorderT userspecialorderT,
			OrderDurationDTO orderDurationDTO) {
		TcpPushPack tcpPushPack = new TcpPushPack();
		tcpPushPack.setTargetUserId(userId);
		tcpPushPack.setBizType((byte) 3);//专车
		tcpPushPack.setModular((short) 0);//推送模块
		tcpPushPack.setCmd((short) 1);//指派订单
		AppointSpecialOrderPushBean pushBean = PushBeanFactory.getAppointSpecialOrderPushBean(userdetail,
				userspecialorderT, orderDurationDTO);
		tcpPushPack.setContent(JSONUtil.toJSONString(pushBean));
		return JSONUtil.toJSONString(tcpPushPack);
	}

	/**
	 * @Description 指派出租车订单的推送
	 * @param userId
	 *            被推送的人的用户id
	 * @param userdetail
	 *            乘客的详情
	 * @param taxiOrdert
	 *            出租车订单信息
	 * @param orderDurationDTO
	 *            订单里程信息
	 * @author fengshuonan
	 * @return String 推送的内容
	 */
	private String appointTaxiOrder(String userId, Userdetail userdetail, UserTaxiOrdert taxiOrdert,
			OrderDurationDTO orderDurationDTO) {
		PushRequest pushRequest = this.getPushRequest(userId);
		pushRequest.setPushType(ClientPushType.msgcontent);
		AppointTaxiOrderPushBean pushBean = PushBeanFactory.appointTaxiOrderPushBean(userdetail, taxiOrdert,
				orderDurationDTO);
		pushRequest.setPushContent(PushDataFactory
				.getCurrentPushDataString(SpecialDriverPushEnum.AppointTaxiOrderPush.toString(), pushBean));
		return JSONUtil.toJSONString(pushRequest);
	}

	/**
	 * @Description:指派订单的推送bean
	 * @author fengshuonan
	 * @since 2.4
	 */
	private String appointSpecialOrder(String userId, Userdetail userdetail, UserspecialorderT userspecialorderT,
			OrderDurationDTO orderDurationDTO) {
		PushRequest pushRequest = this.getPushRequest(userId);
		pushRequest.setPushType(ClientPushType.msgcontent);
		AppointSpecialOrderPushBean pushBean = PushBeanFactory.getAppointSpecialOrderPushBean(userdetail,
				userspecialorderT, orderDurationDTO);
		pushRequest.setPushContent(PushDataFactory
				.getCurrentPushDataString(SpecialDriverPushEnum.AppointSpecialOrderPush.toString(), pushBean));
		return JSONUtil.toJSONString(pushRequest);
	}

	/**
	 * @Description:生成取消订单的推送
	 * @author fengshuonan
	 * @since 2.4
	 */
	private String cancelSpecialOrder(String userId, CustomerCancelSpecialOrderPush customerCancelSpecialOrderPush) {
		PushRequest pushRequest = this.getPushRequest(userId);
		pushRequest.setPushType(ClientPushType.msgcontent);
		pushRequest.setPushContent(PushDataFactory.getCurrentPushDataString(
				SpecialDriverPushEnum.driverCancelSpecialOrderPush.toString(), customerCancelSpecialOrderPush));
		return JSONUtil.toJSONString(pushRequest);
	}

	@Override
	public String getAliasId(String userId) {
		try {
			Specialuserlogin user = baseDaoX.getById(userId, Specialuserlogin.class);
			
			if(user != null){
				return user.getAliasId();
			}
		} catch (Exception e) {
			log.error("获取aliasid错误！SpecialDriverPushRequestFactory ：getAliasId()", e);
		}
		return null;
	}

	@Override
	public ClientAppType getClientType(String userId) {
		try {
			Specialuserlogin user = baseDaoX.getById(userId, Specialuserlogin.class);
			
			if(user != null){
				return ClientAppType.valueOf(user.getClientType());
			}
		} catch (Exception e) {
			log.error("获取aliasid错误！SpecialDriverPushRequestFactory ：getClientType()", e);
		}
		return null;
	}

	@Override
	public PushRequest getPushRequest(String userId) {
		PushRequest pushRequest = new PushRequest();
		pushRequest.setAliasId(this.getAliasId(userId));
		pushRequest.setClientType(this.getClientType(userId));
		pushRequest.setRoleType(RoleType.Driver);
		return pushRequest;
	}

	@Override
	public String createNoticeRequest(String modular, String userId, PushModelBean pushModel) {
		PushRequest pushRequest = getPushRequest(userId);
		pushRequest.setPushType(ClientPushType.msgcontent);
		pushRequest.setPushContent(PushDataFactory.getCurrentPushDataString(modular, pushModel));
		return JSONUtil.toJSONString(pushRequest);
	}
}
